'''
Created on Dec 19, 2012

@author: dheigl
'''
from Stock import Stock
from StockList import StockList
import datetime
import json
import urllib

class GoogleQuote:
    ''' Daily quotes from Google. Date format='yyyy-mm-dd' '''
    def getData(self, symbol, start_date, end_date=datetime.date.today().isoformat()):
        start = datetime.date(int(start_date[0:4]), int(start_date[5:7]), int(start_date[8:10]))
        end = datetime.date(int(end_date[0:4]), int(end_date[5:7]), int(end_date[8:10]))
        for s in symbol:
            s = s.upper()
            print "fetching data for " + s
            url_string = "http://www.google.com/finance/historical?q={0}".format(s)
            url_string += "&startdate={0}&enddate={1}&output=csv".format(
                              start.strftime('%b %d, %Y'), end.strftime('%b %d, %Y'))
            csv = urllib.urlopen(url_string).readlines()
            csv.reverse()
            date = []
            quotes = []
            for bar in xrange(0, len(csv) - 1):
                ds, open_, high, low, close, volume = csv[bar].rstrip().split(',')
                close = float(close)
                '''open_, high, low, close = [float(x) for x in [open_, high, low, close]]'''
                dt = datetime.datetime.strptime(ds, '%d-%b-%y')
                ds = str(dt.strftime("%d.%m.%Y"))  # .split(',')
                # ds = datetime.date(int(year), int(month), int(day))
                '''datetime.datetime.strptime(ds, '%d-%b-%y')'''
                date.append(ds)
                quotes.append(float(close))
            '''
            s = Symbol i.e. GOOG
            date = Date i.e. 29.01.2013
            quotes = values of close i.e. 100.0
            quotes[-1] = last element of close, used for actualValue
            '''
            try:
                stock = Stock(s, date, quotes, quotes[-1])
            except:
                print "UPS da ist was schiefgegangen mit den Aktien..."
            '''WIEDER AUSKOMMENTIEREN!!!'''
            StockList(stock)
        '''TESTDATEN
        StockList(Stock('GRT', ['29.02.2008', '03.03.2008', '04.03.2008', '05.03.2008', '06.03.2008', '07.03.2008'], [10.0, 11.0, 10.5, 11.0, 12.0, 13.0], 15.0))
        StockList(Stock('BOC', ['29.02.2008', '03.03.2008', '04.03.2008', '05.03.2008', '06.03.2008', '07.03.2008'], [8.0, 7.0, 8.0, 9.0, 10.0, 11.0], 14.0))
        StockList(Stock('JOE', ['29.02.2008', '03.03.2008', '04.03.2008', '05.03.2008', '06.03.2008', '07.03.2008'], [10.0, 11.0, 12.0, 12.5, 13.0, 13.5], 20.0))
        StockList(Stock('MAD', ['29.02.2008', '03.03.2008', '04.03.2008', '05.03.2008', '06.03.2008', '07.03.2008'], [15.0, 14.0, 16.0, 15.0, 16.0, 17.0], 21.0))
        '''
        print "fetched"
        return StockList.stockList

if __name__ == '__main__':
    quote = GoogleQuote()
    symbols = ['GOOG', 'MSFT', 'AMZN']
    data = quote.getData(symbols, '2012-01-21')
    for Stock in data:
        print Stock.key
        print Stock.date
        print Stock.quotes
